package czk.concrent.threadstatus;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/5/2 21:56
 */
public class InteruptBeautiful {
    public static void main(String[] args) throws InterruptedException {
        BreakInterupt breakInterupt=new BreakInterupt();
        breakInterupt.start();
        Thread.sleep(3*1000);
        breakInterupt.stop();
    }
}
// 设置监控线程
class BreakInterupt{
    private Thread monitor;
    public void start(){
        monitor=new Thread(()->{
            while (true){
                Thread thread = Thread.currentThread();
                // 判断线程是否被打断
                if (thread.isInterrupted()){
                    System.out.println("料理后事");
                    break;
                }
                System.out.println("监控线程中");
                try {
                    Thread.sleep(1*1000);
                } catch (InterruptedException e) {
                    // 设置线程被打断标记为true，sleep被打断后isinterupt为false
                    thread.interrupt();
                    e.printStackTrace();
                }
            }
        },"t1");
        monitor.start();
    }
    public void stop(){
        monitor.interrupt();
    }
}
